package com.kgc.activityprovider.service.impl;

import com.kgc.activiti.util.ResponseMsg;
import com.kgc.activityprovider.dao.CGWorkFlowMapper;
import com.kgc.activityprovider.feign.cg.CgddOrderFeign;
import com.kgc.activityprovider.feign.cw.WorkBuyPayFeign;
import com.kgc.activityprovider.feign.store.WorkCGRKFeign;
import com.kgc.activityprovider.service.WorkFlowUtilService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("cwPayServiceImpl")
public class CwPayServiceImpl implements WorkFlowUtilService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CGWorkFlowMapper cgWorkFlowMapper;

    @Resource
    private WorkBuyPayFeign workBuyPayFeign;

    @Resource
    private CgddOrderFeign cgddOrderFeign;

    @Resource
    private WorkCGRKFeign workCGRKFeign;

    private final String TABLE = "cw_fk_pay";

    @Override
    public ResponseMsg changeStatus(String code, int status) {
        logger.info("changeStatus:code{},status{}", code,status);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("201");
        Map<String , Object> map = new HashMap<String , Object>();
        map.put("status", status);
        map.put("code", code);
        map.put("table", TABLE);
        int result = cgWorkFlowMapper.checkOrderStatus(map);
        if (result <= 0){
            responseMsg.setMsg("更新失败");
            return responseMsg;
        }
        if (status == 3){
            finishPay(code);
        }
        responseMsg.setCode("200");
        logger.info("changeStatus:code{},status{} ,responseMsg{}" , code,status ,responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertCheck(String code, Integer checkManId, String checkComment) {
        logger.info("insertCheck code{},checkManId{},checkComment{}", code,checkManId,checkComment);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode("201");
        Map<String,Object> map =new HashMap<>();
        map.put("checkManId", checkManId);
        map.put("checkComment", checkComment);
        map.put("code", code);
        map.put("table", TABLE);
        int result = cgWorkFlowMapper.insertCheckCwOtherIncome(map);
        if (result <= 0){
            return responseMsg;
        }
        responseMsg.setCode("200");
        logger.info("insertCheck code{},checkManId{},checkComment{},responseMsg{}", code,checkManId,checkComment,responseMsg);
        return responseMsg;
    }

    boolean finishPay(String code){
        logger.info("finishPay code{}", code);
        Map<String,Object> params =new HashMap<>();
        params.put("code", code);
        com.kgc.basic.util.ResponseMsg res = workBuyPayFeign.getBuyPay(params);
        Map<String,Object> map = (Map<String, Object>) res.getData();
        Object payMoney = map.get("totalAmount");
        params.clear();
        params.put("code", map.get("sourceCode"));
        params.put("typeId",5);
        Map<String,Object> page = (Map<String, Object>) workCGRKFeign.getInStoreList(params).getData();
        ArrayList<Map<String, Object>> cgrk =(ArrayList<Map<String, Object>>) page.get("list");
        map.clear();
        map.put("code", cgrk.get(0).get("sourceCode"));
        map.put("finish_amount", payMoney);
        cgddOrderFeign.updateCGDDData(map);

        //修改采购入库单

        return true;
    }
}
